﻿using System;
using System.IO;
using System.Windows.Forms;

namespace stdCall.File
{
    /// <summary>
    /// 文本文件读写类，File为静态类
    /// </summary>
    class FileHelper
    {
        private static StreamReader readFile;
        private static StreamWriter writeFile;

        public static StreamReader ReadFile
        {
            get { return FileHelper.readFile; }
            set { FileHelper.readFile = value; }
        }

        public static StreamWriter WriteFile
        {
            get { return FileHelper.writeFile; }
            set { FileHelper.writeFile = value; }
        }

        /// <summary>
        /// 打开读文件，未确认是否关闭
        /// </summary>
        /// <param name="filepath"></param>
        /// <returns></returns>
        public  bool openReadFile(String filepath)
        {
            bool flag = false;
            try
            {
                readFile = new StreamReader(filepath);
                flag = true;
            }
            catch (System.Exception ex)
            {
                Console.Write(ex.Message);
            }
            return flag;
        }

        /// <summary>
        /// 打开写文件，未确认是否关闭，默认添加内容到尾部
        /// </summary>
        /// <param name="filepath"></param>
        /// <returns></returns>
        public bool openWriteFile(String filepath)
        {
            bool flag = false;
            try
            {
                writeFile = new StreamWriter(filepath,true);
                flag = true;
            }
            catch (System.Exception ex)
            {
                Console.Write(ex.Message);
            }
            return flag;
        }

        /// <summary>
        /// 打开写文件，可选择是否覆盖
        /// </summary>
        /// <param name="filepath"></param>
        /// <param name="apendFlag">false为覆盖</param>
        /// <returns></returns>
        public bool openWriteFile(String filepath,bool apendFlag)
        {
            bool flag = false;
            try
            {
                writeFile = new StreamWriter(filepath, apendFlag);
                flag = true;
            }
            catch (System.Exception ex)
            {
                Console.Write(ex.Message);
            }
            return flag;
        }

        /// <summary>
        /// 确认是否关闭，并关闭文件
        /// </summary>
        /// <returns></returns>
        public  bool closeFile()
        {
            bool flag = false;
            try
            {
                if (readFile != null)
                {
                    readFile.Close();
                }
                if (writeFile != null)
                {
                    writeFile.Close();
                }
                flag = true;
            }
            catch (System.Exception ex)
            {
                Console.Write(ex.Message);
                flag = false;
            }
            return flag;
        }

        /// <summary>
        /// 读取一行数据
        /// </summary>
        /// <returns></returns>
        public  String readSingleData()
        {
            String data = null;
            try
            {
                if (!ReadFile.EndOfStream)
                {
                    data=readFile.ReadLine().Trim();
                }
            }
            catch (System.Exception ex)
            {
                Console.Write(ex.Message);
            }
            return data;
        }

        /// <summary>
        /// 写入一行数据
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public  bool writeSingleData(String data)
        {
            bool flag = false;
            try
            {
                writeFile.WriteLine(data);
                flag = true;
            }
            catch (System.Exception ex)
            {
                Console.Write("writeSingleData:" + ex.Message);
                ThreadExceptionDialog warnDialog = new ThreadExceptionDialog(ex);
                warnDialog.ShowDialog();
                flag = false;
            }
            return flag;
        }
    }
}
